package com.leetcode.No0809;

import org.junit.Test;

/**
 * @program: expressiveWords
 * @description: 情感丰富的文字
 * @author: Wang Zhihua
 * @date: 2022-11-25
 */
public class Solution {
    public int expressiveWords(String s, String[] words) {
        int ans = 0;
        int len1 = s.length();

        for (String word : words) {
            int len2 = word.length();
            int idx1 = 0, idx2 = 0;
            char ch1, ch2;

            while (true) {
                int n1 = 0, n2 = 0;
                ch1 = s.charAt(idx1);
                while (idx1 < len1 && s.charAt(idx1) == ch1) {
                    ++idx1; ++n1;
                }
                ch2 = word.charAt(idx2);
                while (idx2 < len2 && word.charAt(idx2) == ch2) {
                    ++idx2; ++n2;
                }
                if (ch1 != ch2) {
                    break;
                }
                if (n1 < n2 || (n1 > n2 && n1 < 3)) {
                    break;
                }
                if ((idx1 != len1 && idx2 == len2)
                        || (idx1 == len1 && idx2 != len2)) {
                    break;
                }
                if (idx1 == len1 && idx2 == len2) {
                    ++ans;
                    break;
                }
            }

        }

        return ans;
    }

    @Test
    public void test01() {
        String s;
        String[] words;

        s = "heeellooo";
        words = new String[]{"hello", "hi", "helo"};
        System.out.println(expressiveWords(s, words));

        s = "dddiiiinnssssssoooo";
        words = new String[]{"dinnssoo","ddinso","ddiinnso","ddiinnssoo","ddiinso","dinsoo","ddiinsso","dinssoo","dinso"};
        System.out.println(expressiveWords(s, words));
    }
}
